package com.jk.garden1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jk.garden1.dao.SystemLogDao;
import com.jk.garden1.dao.UserErDao;
import com.jk.garden1.dao.UserMinDao;
import com.jk.garden1.dao.UserVisitorDao;
import com.jk.garden1.entity.*;
import com.jk.garden1.service.UserMinService;
import com.jk.garden1.util.Md5UUIDSaltUtil;
import com.jk.garden1.util.MsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class UserMinServiceImpl extends ServiceImpl<UserMinDao, UserMiniEntity> implements UserMinService {
    @Autowired
    private SystemLogDao systemLogDao;
    @Autowired
    private UserMinDao userMinDao;
    @Autowired
    private UserErDao userErDao;
    @Autowired
    private UserVisitorDao userVisitorDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public UserMiniEntity findByUser(String phone) {
        QueryWrapper<UserMiniEntity> QueryWrapper=new QueryWrapper<>();
        QueryWrapper.eq("phone",phone);
        return userMinDao.selectOne(QueryWrapper);

    }

    @Override
    public void updateUser(UserMiniEntity userMiniEntity) {
         userMinDao.updateUser(userMiniEntity);
    }

    /**
     * 新增二维码信息
     * @param userErEntity
     */
    @Override
    public void addUserEr(UserErEntity userErEntity) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        userErEntity.setUserdate(sdf.format(new Date()));
        userErEntity.setUserstate("正常");
        userErDao.insert(userErEntity);
    }

    /**
     * 新增访客信息
     * @param userVisitorEntity
     */
    @Override
    public void addUserVisitor(UserVisitorEntity userVisitorEntity) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        userVisitorEntity.setUserdate(sdf.format(new Date()));
        userVisitorEntity.setUserstate("正常");
        userVisitorEntity.setId(userVisitorEntity.getId());
        userVisitorDao.insert(userVisitorEntity);
    }

    /**
     * 给员工新增公司码
     * @param userMiniEntity
     */
    @Override
    public void addUserCompany(UserMiniEntity userMiniEntity) {
        userMiniEntity.setUserstate("正常");
        userMinDao.updateById(userMiniEntity);
    }

    /**
     * 查询已经预约的信息
     */
    @Override
    public Object selectList(UserMiniEntity userMini) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",userMini.getUsername());
        return userVisitorDao.selectList(wrapper);
    }

    /**
     * 查询单条数据
     * @param id
     * @return
     */
    @Override
    public UserMiniEntity queryUserOne(String id) {
        return userMinDao.queryUserOne(id);
    }

    @Override
    public UserVisitorEntity queryUserFkOne(String id) {
        return userVisitorDao.queryUserFkOne(id);

    }

    @Override
    public Map registerUser(UserMiniEntity userMiniEntity) {

            //加盐,并将加过盐的密码 和盐重新存入数据库中
            String salt = Md5UUIDSaltUtil.getSalt();
            //自己想搞什么搞什么 加密
            String mySalt=salt.substring(0,5);
            SimpleDateFormat simpleDateFormat;
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String str = simpleDateFormat.format(date);
            String password = Md5UUIDSaltUtil.createMd5Code(userMiniEntity.getPassword()+mySalt);
            userMiniEntity.setPassword(password);

            userMiniEntity.setSalt(salt);
            userMiniEntity.setUserstate("正常");
            userMiniEntity.setId(UUID.randomUUID().toString());
            userMiniEntity.setUsername(userMiniEntity.getUsername());
            userMiniEntity.setPhone(userMiniEntity.getPhone());
            userMinDao.insert(userMiniEntity);

            SystemLogEntity systemLogEntity = new SystemLogEntity();
            systemLogEntity.setOnefunction("系统日志");
            systemLogEntity.setTwofunction("用户管理");
            systemLogEntity.setLogcontent("注册新用户");
            systemLogEntity.setCreatetime(str);
            systemLogDao.insert(systemLogEntity);

        // redisTemplate.delete(key);
        QueryWrapper<UserMiniEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",userMiniEntity.getPhone());
        UserMiniEntity user = userMinDao.selectOne(wrapper);


        String token = UUID.randomUUID().toString().replaceAll("-", "");
        redisTemplate.opsForValue().set(token, userMiniEntity.getPhone(), 3600, TimeUnit.SECONDS);
        user.setToken(token);
        user.setPassword(userMiniEntity.getPassword());
        return MsgUtil.returnMsg(200,"注册成功",user);
    }


}
